angular.module("NTApp", ['ui.router'])
  .config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/DevicesList');
    $stateProvider
      .state('DevicesList', {
        url: '/DevicesList',
        views: {
          'content': {
            templateUrl: 'scripts/views/devicesList.html',
            controller: 'DevicesListCtrl as l'
          }
        }
      })
      .state('DeviceDetail', {
        url: '/DeviceDetail/:idStr',
        views: {
          'content': {
            templateUrl: 'scripts/views/deviceDetail.html',
            controller: 'DeviceDetailCtrl as d'
          }
        }
      })
      .state('DeviceBinding', {
        url: '/DeviceBinding/:idStr',
        views: {
          'content': {
            templateUrl: 'scripts/views/deviceBinding.html',
            controller: 'DeviceBindingCtrl as b'
          }
        }
      })
      .state('ContactUs', {
        url: '/ContactUs',
        views: {
          'content': {
            templateUrl: 'scripts/views/contactUs.html',
            controller: 'ContactUsCtrl as c'
          }
        }
      })
      .state('ScanDeviceId', {
        url: '/ScanDeviceId',
        views: {
          'content': {
            templateUrl: 'scripts/views/ScanDeviceId.html',
            controller: 'ScanDeviceIdCtrl as s'
          }
        }
      })
      .state('ShareDevice', {
        url: '/ShareDevice/:idStr',
        views: {
          'content': {
            templateUrl: 'scripts/views/shareDevice.html',
            controller: 'ShareDeviceCtrl as sd'
          }
        }
      })
  })
  .run(function($rootScope, $state, WXService) {

    $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {

      if (toState.name == 'DeviceBinding' && fromState.name == 'DevicesList') {
        wx.closeWindow();
      }

      if (toState.name == 'ScanDeviceId' && fromState.name == 'DevicesList' && !WXService.getListToScan()) {
        wx.closeWindow();
      }

      if (toState.name == 'DeviceDetail' && fromState.name == 'DevicesList' && !WXService.getListToDetail()) {
        wx.closeWindow();
      }
    });
  });
